//
// Created by Administrator on 2023/4/15.
//

#ifndef WEBRTC_MODEL_SINGLEFRAMESINGLEPACKETANALYZE_H
#define WEBRTC_MODEL_SINGLEFRAMESINGLEPACKETANALYZE_H

#include <bits/stdc++.h>

#include "StuckTimePredictor.h"

using namespace std;

class SingleFrameSinglePacketAnalyze {
private:
    //平均帧率
    double avgFrameRate;
    //帧平均包个数
    double avgFramePacketCount;
    //卡顿时间阈值
    int stuckTimeThreshold;
    //到达时间间隔分布
    vector<pair<long long, double>> intervalDistribution;
    //卡顿时间预测器
    StuckTimePredictor stuckTimePredictor;
    //帧数据分析
    FrameAnalyze frameAnalyze;

public:
    SingleFrameSinglePacketAnalyze() :
            avgFrameRate(getAvgFrameRate()),
            avgFramePacketCount(getAvgFramePacketCount()),
            intervalDistribution(getIntervalDistribution()),
            stuckTimeThreshold(50),
            stuckTimePredictor(avgFrameRate, avgFramePacketCount, stuckTimeThreshold, intervalDistribution) {};

    /**
     * 读取文件获取平均帧率
     * @return 平均帧率
     */
    double getAvgFrameRate() {
        return frameAnalyze.getAvgFrameRate();

    }

    /**
     * 帧平均包个数
     * @return 平均包个数，这里因为是单帧单包，所以是1
     */
    static double getAvgFramePacketCount() {
        return 1;
    }

    /**
     * 读取文件获取到达时间的间隔的概率分布，用于生成随机数
     * @return 概率分布
     */
    static vector<pair<long long int, double>> getIntervalDistribution() {
        vector<pair<long long, double>> distribution;
        ifstream intervalDistributionInput;
        intervalDistributionInput.open("log/receiveDistribution.csv", ios::in);
        string line;
        while (getline(intervalDistributionInput, line)) {
            auto splitIndex = line.find(',');
            if (splitIndex != std::string::npos) {
                auto interval_ = strtoll(line.substr(0, splitIndex).c_str(), nullptr, 10);
                auto percent_ = strtod(line.substr(splitIndex + 1, line.size() - splitIndex).c_str(), nullptr);
                cout << interval_ << "," << percent_ << endl;
                distribution.emplace_back(interval_, percent_);
            }
        }
        intervalDistributionInput.close();
        return distribution;
    }

    //分析单帧单包的预测效果
    void analyze() {

    }

};


#endif //WEBRTC_MODEL_SINGLEFRAMESINGLEPACKETANALYZE_H
